package com.uni.sofia.reader.presentation.test
{
	import com.uni.sofia.reader.magazine.Magazine;
	import com.uni.sofia.reader.magazine.MagazinesVO;
	import com.uni.sofia.reader.presentation.LibraryPresentation;
	import com.uni.sofia.reader.utils.MagazineBuilder;
	
	import flexunit.framework.Assert;
	
	public class LibraryPresentationTest
	{		
		
		private var libraryPresentation:LibraryPresentation;
		
		[Before]
		public function setUp():void
		{
			libraryPresentation = new LibraryPresentation();
		}
		
		[Test]
		public function testSize():void
		{
			libraryPresentation.addMagazine(MagazineBuilder.loadMagazine(MagazinesVO.empireMagazine));
			Assert.assertEquals(1, libraryPresentation.magazines.length);
			libraryPresentation.addMagazine(MagazineBuilder.loadMagazine(MagazinesVO.empireMagazine));
			Assert.assertEquals(2, libraryPresentation.magazines.length);
			libraryPresentation.addMagazine(MagazineBuilder.loadMagazine(MagazinesVO.empireMagazine));
			Assert.assertEquals(3, libraryPresentation.magazines.length);
		}
		[Test]
		public function testSortByDate():void
		{
			var times:Magazine = MagazineBuilder.loadMagazine(MagazinesVO.timesMagazine);
			var playboy:Magazine = MagazineBuilder.loadMagazine(MagazinesVO.playboyMagazine);
			
			libraryPresentation.addMagazine(times);
			libraryPresentation.addMagazine(playboy);
			
			Assert.assertEquals(times, libraryPresentation.magazines.getItemAt(0));
			libraryPresentation.sortByDate();
			Assert.assertEquals(playboy, libraryPresentation.magazines.getItemAt(0));
		}
		
		[Test]
		public function sortByName():void
		{
			var times:Magazine = MagazineBuilder.loadMagazine(MagazinesVO.timesMagazine);
			var playboy:Magazine = MagazineBuilder.loadMagazine(MagazinesVO.playboyMagazine);
			var empire:Magazine = MagazineBuilder.loadMagazine(MagazinesVO.empireMagazine);
			
			libraryPresentation.addMagazine(playboy);
			libraryPresentation.addMagazine(times);
			libraryPresentation.addMagazine(empire);
			
			Assert.assertEquals(playboy, libraryPresentation.magazines.getItemAt(0));
			libraryPresentation.sortByName();
			Assert.assertEquals(empire, libraryPresentation.magazines.getItemAt(0));
			Assert.assertEquals(times, libraryPresentation.magazines.getItemAt(1));
			Assert.assertEquals(playboy, libraryPresentation.magazines.getItemAt(2));
		}
		
		[Test]
		public function sortByPublisher():void
		{
			var times:Magazine = MagazineBuilder.loadMagazine(MagazinesVO.timesMagazine);
			var playboy:Magazine = MagazineBuilder.loadMagazine(MagazinesVO.playboyMagazine);
			var empire:Magazine = MagazineBuilder.loadMagazine(MagazinesVO.empireMagazine);
			
			libraryPresentation.addMagazine(playboy);
			libraryPresentation.addMagazine(times);
			libraryPresentation.addMagazine(empire);
			
			Assert.assertEquals(playboy, libraryPresentation.magazines.getItemAt(0));
			libraryPresentation.sortByPulisher();
			Assert.assertEquals(times, libraryPresentation.magazines.getItemAt(0));
			Assert.assertEquals(empire, libraryPresentation.magazines.getItemAt(1));
			Assert.assertEquals(playboy, libraryPresentation.magazines.getItemAt(2));
		}
		
		[Ignore]
		[Test]
		public function sortBySizeDescending():void
		{
			var times:Magazine = MagazineBuilder.loadMagazine(MagazinesVO.timesMagazine);
			var playboy:Magazine = MagazineBuilder.loadMagazine(MagazinesVO.playboyMagazine);
			
			libraryPresentation.addMagazine(times);
			libraryPresentation.addMagazine(playboy);
			
			Assert.assertEquals(times, libraryPresentation.magazines.getItemAt(0));
			libraryPresentation.sortBySizeDescending();
			Assert.assertEquals(playboy, libraryPresentation.magazines.getItemAt(0));
			Assert.assertEquals(times, libraryPresentation.magazines.getItemAt(1));
		}
		
		[Test]
		public function filter():void
		{
			var times:Magazine = MagazineBuilder.loadMagazine(MagazinesVO.timesMagazine);
			var playboy:Magazine = MagazineBuilder.loadMagazine(MagazinesVO.playboyMagazine);
			var empire:Magazine = MagazineBuilder.loadMagazine(MagazinesVO.empireMagazine);
			var ew:Magazine = MagazineBuilder.loadMagazine(MagazinesVO.entertainmentWeeklyMagazine);
			
			libraryPresentation.addMagazine(times);
			libraryPresentation.addMagazine(playboy);
			libraryPresentation.addMagazine(empire);
			libraryPresentation.addMagazine(ew);
			
			libraryPresentation.setFilter(["Amazon"]);
			Assert.assertEquals(1, libraryPresentation.magazines.length);
			
			libraryPresentation.setFilter(["Amazon", "Internet"]);
			Assert.assertEquals(2, libraryPresentation.magazines.length);
		}
		
		[Test]
		public function publishers():void
		{
			var times:Magazine = MagazineBuilder.loadMagazine(MagazinesVO.timesMagazine);
		    var ew:Magazine = MagazineBuilder.loadMagazine(MagazinesVO.entertainmentWeeklyMagazine);
			
			libraryPresentation.addMagazine(times);
			libraryPresentation.addMagazine(ew);
			
			Assert.assertEquals("Amazon", libraryPresentation.publishers.getItemAt(0));
			Assert.assertEquals("Internet", libraryPresentation.publishers.getItemAt(1));
		}
		
	}
}